﻿using Max.PH5_2011A.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH5_2011A.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BaseController<T> : ControllerBase where T : class,new()
    {
        IBaseService<T> _serice;

        public BaseController(IBaseService<T> serice)
        {
            _serice = serice;
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPost]
        public virtual IActionResult Insert(T t)
        {
            _serice.Insert(t);
            return Ok();
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpPut]
        public virtual IActionResult Update(T t)
        {
            _serice.Update(t);
            return Ok();
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpDelete]
        public virtual IActionResult Delete(T t)
        {
            _serice.Delete(t);
            return Ok();
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpGet]
        public virtual IActionResult Query()
        {
            var list = _serice.Query();
            return Ok(new { code = 200,data = list});
        }

        /// <summary>
        /// 查询一条数据
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        [HttpGet("first")]
        public virtual IActionResult QueryFirst(string id)
        {
            var model = _serice.QueryFirst(id);
            return Ok(model);
        }

    }
}
